import { ENUM_CLIP } from '../GbzEnum';
import GbzDataManager from './GbzDataManager';
import GbzResManager from "./GbzResManager"

export default class GbzAudioManager {
    private as: cc.AudioSource = null
    private loop: boolean = true
    private volume: number = 0.3
    private static _ins: any = null

    static getIns<T>(): T {
        if (this._ins === null) {
            this._ins = new GbzAudioManager()
            this._ins.init()
        }
        return this._ins
    }

    static get ins() {
        return this.getIns<GbzAudioManager>()
    }

    init() {
        this.as = new cc.AudioSource()
        this.as.loop = this.loop
        this.as.volume = this.volume
    }

    async play(name: ENUM_CLIP | string = null, loop: boolean = false) {
        if (name) {
            if (!GbzDataManager.ins.game_sound) return
            const clip = await GbzResManager.ins.clip(name)
            return cc.audioEngine.playEffect(clip, loop)
        } else {
            if (!GbzDataManager.ins.game_music) return
            if (this.as.clip) {
                this.as.play()
                return
            }
            const clip = await GbzResManager.ins.clip(ENUM_CLIP.GAME_BGM)
            this.as.clip = clip
            this.as.play()
        }
    }

    stop(aid: number = -1) {
        if (aid >= 0) {
            cc.audioEngine.stopEffect(aid)
        } else {
            this.as.stop()
        }
    }
}
